package org.groupg.practice.guiPractice.awt;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

public class CopyPerson {
    Frame f = new Frame("复制对象");
    Button copy = new Button("复制");
    Button paste = new Button("粘贴");
    TextField name = new TextField(15);
    TextField age = new TextField(15);
    TextArea ta = new TextArea(3, 30);
    // 创建本地剪贴板
    Clipboard clipboard = new Clipboard("cp");

    public static void main(String[] args) {
        new CopyPerson().init();
    }

    public void init() {
        var p = new Panel();
        p.add(new Label("姓名"));
        p.add(name);
        p.add(new Label("年龄"));
        p.add(age);
        f.add(p, BorderLayout.NORTH);
        f.add(ta);
        var bp = new Panel();
        // 为“复制”按钮添加事件监听器
        copy.addActionListener(e -> copyPerson());
        // 为“粘贴”按钮添加事件监听器
        paste.addActionListener(e ->
        {
            try {
                readPerson();
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        });
        bp.add(copy);
        bp.add(paste);
        f.add(bp, BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
    }

    public void copyPerson() {
        // 以name、age文本框的内容创建Person对象
        var p = new Person(name.getText(),
                Integer.parseInt(age.getText()));
        // 将Person对象封装成LocalObjectSelection对象
        var ls = new LocalObjectSelection(p);
        // 将LocalObjectSelection对象放入本地剪贴板中
        clipboard.setContents(ls, null);
    }

    public void readPerson() throws Exception {
        // 创建保存Person对象引用的DataFlavor对象
        var personFlavor = new DataFlavor(
                "application/x-java-jvm-local-objectref; class=org.groupg.practice.jdk17practice.guiPractice.RecordPerson");
        // 取出本地剪贴板中的内容
        if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
            var p = (Person) clipboard.getData(personFlavor);
            ta.setText(p.toString());
        }
    }

    public class LocalObjectSelection implements Transferable {
        // 持有一个对象的引用
        private final Object obj;

        public LocalObjectSelection(Object obj) {
            this.obj = obj;
        }

        // 返回该Transferable对象支持的DataFlavor
        public DataFlavor[] getTransferDataFlavors() {
            var flavors = new DataFlavor[2];
            // 获取被封装对象的类型
            Class clazz = obj.getClass();
            String mimeType = "application/x-java-jvm-local-objectref; "
                    + "class=" + clazz.getName();
            try {
                flavors[0] = new DataFlavor(mimeType);
                flavors[1] = DataFlavor.stringFlavor;
                return flavors;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        }

        // 取出该Transferable对象封装的数据
        public Object getTransferData(DataFlavor flavor)
                throws UnsupportedFlavorException {
            if (!isDataFlavorSupported(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            if (flavor.equals(DataFlavor.stringFlavor)) {
                return obj.toString();
            }
            return obj;
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavor.equals(DataFlavor.stringFlavor) ||
                    flavor.getPrimaryType().equals("application")
                            && flavor.getSubType().equals("x-java-jvm-local-objectref")
                            && flavor.getRepresentationClass().isAssignableFrom(obj.getClass());
        }
    }


}